package utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import Entity.BuildersDBEntity;
import Entity.BuildersDBEntity;
import Entity.ManufacturesDBEntity;
import Entity.ManufacturyEntity;
import Entity.OfficesDBEntity;
import Entity.OfficesDBEntity;
import android.util.Xml;


public class XmlOfficesParser {
	 private static final String ns = null;
	 public OfficesDBEntity parse(InputStream in) throws XmlPullParserException, IOException {   
		 try {
         XmlPullParser parser = Xml.newPullParser();
         parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);       
         parser.setInput(in , null);
         parser.nextTag();       
         return readOfficesXml(parser); 
	 } finally {
         in.close();
     }
 }
	 
	 private OfficesDBEntity readOfficesXml(XmlPullParser parser) throws XmlPullParserException, IOException {
		 parser.require(XmlPullParser.START_TAG, ns, "data");
		 OfficesDBEntity offices = new OfficesDBEntity();
		 while (parser.next() != XmlPullParser.END_TAG) {
			 if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
			 String name = parser.getName();
			 if (name.equals("officegroupcode")) {
				 offices.setOfficegroupcode(readOfficegroupcode(parser)) ;
				 
			 }
			 else if  (name.equals("offices")) {
				 offices.setOffices(readOffices(parser)) ;
				 
				 
			 }
	            
	            else{
	            	XmlTemplateParser.skip(parser);
	            }
		 }
		 return offices;
	 }
	 

	 private String  readOfficegroupcode (XmlPullParser parser)throws IOException, XmlPullParserException {
		 parser.require(XmlPullParser.START_TAG, ns, "officegroupcode");
		 String text = XmlTemplateParser.readText(parser);
	      parser.require(XmlPullParser.END_TAG, ns, "officegroupcode");
		 return  text;
		 
	 }
	 
	 private ArrayList<MyFifthSet> readOffices(XmlPullParser parser) throws IOException, XmlPullParserException {
	    	ArrayList<MyFifthSet> list= new ArrayList<MyFifthSet>();
	        parser.require(XmlPullParser.START_TAG, ns, "offices");
	        while (parser.next() != XmlPullParser.END_TAG) {
	            if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
	            String name = parser.getName();
	            if (name.equals("office")) {
	            	 list.add(readOffice(parser)) ;}
	            
	        	 else {
	        		 XmlTemplateParser.skip(parser);
	            }
	        }

	        return list;
	    }
	 private OrderedFifthSet<String, String ,String ,String ,String> readOffice (XmlPullParser parser)throws IOException, XmlPullParserException {
		 String key = null ; 
		 String value1 = null ; 
		 String value2 = null ; 
		 String value3 = null ; 
		 String value4 = null ; 
		 parser.next();
	     parser.next();
		 parser.require(XmlPullParser.START_TAG, ns, "officename");
	      key= XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "officename");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "officecode");     
	        value1 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "officecode");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "officeunit");     
	        value2 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "officeunit");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "officeunitcode");     
	        value3 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "officeunitcode");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "officeprice");     
	        value4 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "officeprice");
	        parser.next();
	        parser.next();
	        OrderedFifthSet <String, String, String , String , String> office = new  OrderedFifthSet<String, String, String ,String ,String>(key,value1,value2,value3,value4);
		 return  office;

	 }
	 
	 
	  
	
}
